/***********************************************
 This file is part of redia-library-app-ios.
 
 Copyright (c) 2012, 2013 Redia A/S
 
 Redia-library-app-ios is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 Redia-library-app-ios is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with redia-library-app-ios.  If not, see <http://www.gnu.org/licenses/>.
 
 *********************************************** */


#import "IGLanguageManager.h"
//#import "ChannelFetchManager.h"

#define IGLANGUAGEMANAGER_CHANGE_NOTIFICATION_NAME @"IG-language-changed"


@implementation IGLanguageManager

@synthesize currentLang;


static IGLanguageManager *sharedSingleton;

+ (void)initialize
{
    static BOOL initialized = NO;
    if(!initialized)
    {
        initialized = YES;
        sharedSingleton = [[IGLanguageManager alloc] init];
    }
}

+ (IGLanguageManager*)instance
{
	return sharedSingleton;
}


- (id)init {
    self = [super init];
    if (self) {
        currentLangBundle = [NSBundle mainBundle];
    }
    return self;
}

- (void)setNewLanguage:(NSString *)newlang
{
    currentLang=newlang;
    [self setLanguageBundle:newlang];
    [[NSNotificationCenter defaultCenter] postNotificationName:IGLANGUAGEMANAGER_CHANGE_NOTIFICATION_NAME object:self userInfo:nil];
}

- (void)registerForLanguageNotification:(id)obj withSelector:(SEL)selector
{
    [[NSNotificationCenter defaultCenter] addObserver:obj selector:selector name:IGLANGUAGEMANAGER_CHANGE_NOTIFICATION_NAME object:self];
}

- (void)unregisterForLanguageNotification:(id)obj
{
    [[NSNotificationCenter defaultCenter] removeObserver:obj];
}

- (NSString *)getCurrentIGLangID
{
#ifdef LIB_INFOOBJECT_SUPPORT_OLD_LANGUAGE_NAMING
    if ([[ChannelFetchManager instance] isUsingNewBackend]) {
        return self.currentLang;
    }

    //hack for old IG backends
    if ([self.currentLang isEqualToString:@"da"]) {
        return @"dk";
    } else if ([self.currentLang isEqualToString:@"en"]) {
        return @"uk";
    } else {
        NSLog(@"WARNING: unknown language selection for InfoGalleri database");
    }
    return @"dk"; //FIXME: which language fall back?
#else
    return self.currentLang;
#endif
}


// Gets the current localized string as in NSLocalizedString.
//
// example calls:
// AMLocalizedString(@"Text to localize",@"Alternative text, in case the other is not found");
- (NSString *)localizedStringForKey:(NSString *)key value:(NSString *)comment
{
	return [currentLangBundle localizedStringForKey:key value:comment table:nil];
}


// Sets the desired language of the ones you have.
// example calls:
// setLanguage:@"Italian";

// If this function is not called it will use the default OS language.
// If the language does not exists returns the default OS language.
- (void) setLanguageBundle:(NSString*) l{
	NSLog(@"preferredLang: %@", l);
	
	NSString *path = [[ NSBundle mainBundle ] pathForResource:l ofType:@"lproj" ];
    
	if (path == nil) {
		//in case the language does not exist
		[self resetLocalization];
    }
	else {
		currentLangBundle = [NSBundle bundleWithPath:path];
    }
}

- (NSString*) getPreferredLanguage{
    
	NSArray* languages = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"];
	NSString *preferredLang = [languages objectAtIndex:0];
    
	return preferredLang;
}

// Resets the localization system, so it uses the OS default language.
- (void) resetLocalization
{
	currentLangBundle = [NSBundle mainBundle];
}

@end

